package org.glut.config;

import org.glut.service.MyUserDatailsServeice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.filter.CharacterEncodingFilter;

import java.util.Objects;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //指定密码加密方式
    @Autowired
    private MyUserDatailsServeice myUserDatailsServeice;

    @Bean
    public PasswordEncoder passwordEncoder() {

//        return new BCryptPasswordEncoder();
        return new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                return charSequence.toString();
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                return Objects.equals(charSequence.toString(), s);
            }
        };
    }

    @Override
    //授权
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/user/*").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3")
                .antMatchers("/level4/**").hasRole("vip4")
                .antMatchers("/level5/**").hasRole("vip5");
        http.formLogin()
                .usernameParameter("username")
                .passwordParameter("password")
                .loginPage("/toLogin")
                .loginProcessingUrl("/login").permitAll(); // 登陆表单提交请求
        http.logout().logoutSuccessUrl("/");
        http.csrf().disable();      //关闭csrf
        http.rememberMe().rememberMeParameter("remember");
        //解决中文乱码问题
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        //http.addFilterBefore(filter,CsrfFilter.class);
    }

    @Override
    //认证
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这些数据正常应该中数据库中读
//       auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
//               .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3","vip4","vip5")
//               .and().withUser("test").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2");
      //使用内存的方式
//        auth.inMemoryAuthentication().withUser("admin").password("123456").roles("vip1","vip2","vip3","vip4","vip5")
//                .and().withUser("test").password("123456").roles("vip1","vip2");
        auth.userDetailsService(myUserDatailsServeice);
    }
}
